package com.blog.blogservice.util;

import org.springframework.data.redis.core.RedisTemplate;

/**
 * Redis 自定义方法
 *
 * @author guanglin.ma
 * @date 2024-11-13 23:38
 */
public class RedisUtils {

    private static boolean inTransaction = false;


    /**
     * 启动事务
     *
     * @param
     */
    public static void startTransaction(RedisTemplate redisTemplate) {
        redisTemplate.setEnableTransactionSupport(true);
        if (!inTransaction) {
            redisTemplate.multi();
            inTransaction = true;
        }
    }

    /**
     * 取消事务
     *
     * @param
     */
    public static void discardTransaction(RedisTemplate redisTemplate) {
        if (inTransaction) {
            redisTemplate.discard();
            inTransaction = false;
        }
    }

    /**
     * 判断是否启动事务
     *
     * @return
     */
    public static boolean isInTransaction() {
        return inTransaction;
    }

    public static void execTransaction(RedisTemplate redisTemplate){
        redisTemplate.exec();
    }
}
